﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using JeanFish.Tracing.Configuration;
using System.Configuration;
namespace JeanFish.Tracing
{
    /// <summary>
    /// 提供标准日志功能。
    /// </summary>
    public static class Log
    {
        public static TraceSource Source
        {
            private set;
            get;
        }
        static Log()
        {
            TracingConfiguration setting = ConfigurationManager.GetSection("tracingSetting") as TracingConfiguration;
            if (setting == null) setting = new TracingConfiguration();
            if (string.Compare(setting.SourceName, TracingConfiguration.DefaultSourceName, true) == 0)
            {
                Source = new System.Diagnostics.TraceSource(Process.GetCurrentProcess().ProcessName);
            }
            else
            {
                Source = new System.Diagnostics.TraceSource(setting.SourceName);
            }
        }
        public static void Write(TraceEventType eventType, int id)
        {
            Source.TraceEvent(eventType, id);
        }
        public static void Write(TraceEventType eventType, int id, string message)
        {
            Source.TraceEvent(eventType, id, message);
        }
        public static void Write(TraceEventType eventType, int id, string message, params object[] args)
        {
            Source.TraceEvent(eventType, id, message, args);
        }
        public static void Information(string message)
        {
            Source.TraceInformation(message);
        }
        public static void Information(string format, params object[] args)
        {
            Source.TraceInformation(format, args);
        }
        public static void Write(TraceEventType eventType, int id,params object[] data)
        {
            //if (data.Length == 0)
            //    Source.TraceEvent(eventType, id);
            if (data.Length == 1)
                Source.TraceData(eventType, id, data[0]);
            if (data.Length > 1)
                Source.TraceData(eventType, id, data);
        }
        public static void Write()
        {
            Source.Flush();
        }
        public static void WriteLog(this Exception exception, int id, TraceEventType eventType = TraceEventType.Error)
        {
            Write(eventType, id, exception);
        }
    }
}
